Add sparse tensor primitives with einsum contraction#48
Closed
imlvts wants to merge 6 commits intotrueagi-io:mainfrom
Closed
Add sparse tensor primitives with einsum contraction#48imlvts wants to merge 6 commits intotrueagi-io:mainfrom
imlvts wants to merge 6 commits intotrueagi-io:mainfrom
Conversation
Integrate N-dimensional sparse tensors into MORK expressions: - SparseTensorF64: PathMap-backed sparse tensor with BOB encoding, arbitrary rank, element-wise add/mul via lattice operations - einsum-dyn: copied into workspace as local crate, runtime Einstein summation with VM compiler supporting arbitrary N-D specs - Tensor sinks (tensor_collect, tensor_einsum, tensor_add, tensor_mul, tensor_free) using WriteResource::TensorStore through the standard resource infrastructure — no thread-locals - tensor_get/tensor_nnz pure functions via ExprSource.context pointer propagated through EvalScope - End-to-end test: load matrix data as S-expressions, collect into tensors via exec, einsum multiply, verify result MeTTa usage: (exec P1 (, (a $r $c $v)) (O (tensor_collect A $r $c $v))) (exec P2 (,) (O (tensor_einsum "ab,bc->ac" A B C))) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- tensor_get now parses indices from string symbols (not binary u32), matching how MeTTa S-expressions encode numbers - End-to-end test exercises full pipeline: load data → tensor_collect → tensor_einsum → tensor_get to verify C[0,0]=19.0 and C[1,1]=50.0 - TensorEinsumSink strips quotes from spec string - Command sinks (einsum, binop, free) parse args in new() for literal templates, or in sink() for variable-bound templates Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The opaque context pointer now flows through the extern "C" call signature rather than being set on each ExprSource before dispatch. tensor_get/tensor_nnz read it directly from the arg; EvalScope.context and ASink::set_context remain the owner-facing knob. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The `parsed` guard in `sink()` captured only the first pattern match and dropped subsequent ones, so `(exec (, (to_free $n)) (O (tensor_free $n)))` freed one tensor regardless of how many facts matched. Collect all names across `sink()` calls and drain them in `finalize()`. The test exercises three concurrent matches with one non-match that must survive. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Both pass — the basic TensorBinopSink path works for constant names under an empty `(,)` pattern. Does not yet cover variable-bound names or multi-match dispatch, which are the likelier breakage surfaces hinted at by the todo comment. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Drop the `entries: Vec<(Vec<usize>, f64)>` buffer — parse each matched tuple in `sink()` and call `SparseTensorF64::set` on the stored tensor directly. The first match replaces any existing tensor at `name` with a fresh one of the sink's rank, so repeated exec invocations start clean; subsequent matches in the same invocation accumulate. Tracked with one `initialized: bool` instead of the entry Vec. Safe as long as no concurrent tensor-as-source path reads `name` during the exec. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Integrate N-dimensional sparse tensors into MORK expressions:
MeTTa usage: